package com.sf.fy.uav.user.rest;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sf.encryption.machine.config.SfUserDataEncrypt;
import com.sf.fy.uav.common.enums.PlatformCodeEnum;
import com.sf.fy.uav.common.request.common.IdRequest;
import com.sf.fy.uav.common.request.operate.UserInfoReq;
import com.sf.fy.uav.common.response.user.UserDetailResponse;
import com.sf.fy.uav.common.response.user.UserResponse;
import com.sf.fy.uav.common.utils.PhoneEncoderUtil;
import com.sf.fy.uav.common.utils.Rs;
import com.sf.fy.uav.user.entity.User;
import com.sf.fy.uav.user.mapper.UserMapper;
import com.sf.fy.uav.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author LTQ
 * @since 2023-04-03
 */
@RestController
@RequestMapping("/sys/user")
public class UserRest {

    @Autowired
    private IUserService iUserService;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private PhoneEncoderUtil phoneEncoderUtil;

    public UserResponse getByUserId() {
        return iUserService.getByUserId();
    }

    public UserResponse getByUserId(String userId) {
        return iUserService.getByUserId(userId);
    }

    public List<UserDetailResponse> getUsers(Integer page, Integer size, String phoneNumber, Integer platformCode) {
        return iUserService.getUsers(page, size, phoneNumber, platformCode);
    }

    public User getUser() {
        return iUserService.getUser();
    }


    public Long getTotal(@RequestParam(required = false) String phoneNumber, @RequestParam(required = false) Integer platformCode) {
        return iUserService.getTotal(phoneNumber, platformCode);
    }


    @PostMapping("freeze")
    public void freeze(@RequestBody IdRequest idRequest) {
        iUserService.freeze(idRequest);
    }

    @PostMapping("restore")
    public void restore(@RequestBody IdRequest idRequest) {
        iUserService.restore(idRequest);
    }


    public User getNormalUser(String userId) {
        return iUserService.getNormalUser(userId);
    }


    public User getOne(String phoneNumber, String countryCode, Integer platformCode) {
        return iUserService.getOne(phoneNumber, countryCode, platformCode);
    }

    public List<User> getList(String phoneNumber, String countryCode, List<Integer> platformCodes) {
        return iUserService.getList(phoneNumber, countryCode, platformCodes);
    }

    public Map<String, String> getUserInfoByUserIdOrPhone(UserInfoReq userInfoReq) {
        Set<String> phoneSetReq = new HashSet<>();
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (userInfoReq.getPhoneArray() != null && userInfoReq.getPhoneArray().length > 0) {
            for (String phone : userInfoReq.getPhoneArray()) {
                phoneSetReq.add(phoneEncoderUtil.encryptString(phone));
            }
            lambdaQueryWrapper.select(User::getId, User::getPhoneNumber).in(User::getPlatformCode, 1, PlatformCodeEnum.CX.getCode()).in(User::getPhoneNumber, phoneSetReq);
        }
        if (!CollectionUtils.isEmpty(userInfoReq.getUserIds())) {
            lambdaQueryWrapper.select(User::getId, User::getPhoneNumber).in(User::getPlatformCode, 1, PlatformCodeEnum.CX.getCode()).in(User::getId, userInfoReq.getUserIds());
        }
        return userMapper.selectList(lambdaQueryWrapper).stream().collect(Collectors.toMap(User::getId, User::getPhoneNumber, (value1, value2) -> value1));
    }

}
